package com.dkd.day20250705;
import java.util.*;

public class Main4 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        String[] s1 = s.split(" ");
        int[] a = new int[s1.length];
        for (int i = 0; i < s1.length; i++) {
            a[i] = Integer.parseInt(s1[i]);
        }
        int answer = solve(a,0);
        System.out.println(answer);

    }


    public static int solve(int[] arr, int n) {
        int node = arr[n]==-1?0:arr[n];
        int numleft = 0;
        int numright = 0;
        if(2*n+1 < arr.length) {
            numleft += solve(arr, 2*n+1);
        }
        if(2*n+2 < arr.length) {
          numright +=  solve(arr, 2*n+2);
        }
        return node +Math.max(numleft, numright);
    }
}
